<?php

namespace Wulian\Controller;

use Common\Controller\AdminbaseController;

class AdminCardController extends AdminbaseController {

    protected $m_card,$m_user,$m_card_activate,$m_card_import,$m_package,$m_card_status,$m_flow_record,$m_operate_log;

    function _initialize() {
        parent::_initialize();
        $this->m_card= D("Wulian/WlCard");
        $this->m_user = M("Users");
        $this->m_card_activate = M("WlCardActivate");
        $this->m_card_import = M("WlCardImport");
        $this->m_package = M("WlPackage");
        $this->m_card_status = M("WlCardStatus");
        $this->m_flow_record = M("WlFlowRecord");
        $this->m_operate_log = D("WlOperateLog");
    }



    /**
     * 会员导入
     */
    public function import(){
        $this->display();
    }

    /**
     * 会员卡列表
     */
    public function card_list(){

        $where = array();
        $s_number = I("s_number");

        if(!empty($s_number)){
            $_GET["s_number"] = $s_number;
            $this->assign("s_number",$s_number);
            $where["card_iccid"] = $s_number;
            checkCardNumber($s_number);
        }
        $s_card_number = I("s_card_number");
        if(!empty($s_card_number)){
            $_GET["s_card_number"] = $s_card_number;
            $this->assign("s_card_number",$s_card_number);
            $where["card_number"] = $s_card_number;
        }

        $count=$this->m_card->where($where)->count();
        $page = $this->page($count, 50);
        $list = $this->m_card->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 图片下载
     */
//    public function download_qrcode(){
//        $img_data = I("img_data");
//        if(empty($img_data)){
//            $this->error("图片数据为空！");exit;
//        }
//        header("Content-type: application/octet-stream");
//        header('Content-Disposition: attachment; filename="' . $img_data . '"');
//        header("Content-Length: ". filesize($img_data));
//        readfile($img_data);
//    }

    /**
     * 会员数据导入
     */
    public function import_post(){

        $file = $_FILES['card_file']['tmp_name'];
        $file_type=$_FILES['card_file']['type'];

        if (!in_array($file_type, getExcelType())) {
            $this->error("文件格式有误！");
            exit;
        }
        if(empty($file)){
            $this->error("文件不存在！");exit;
        }
        $excel = new \Common\Lib\excel($file);
        $excel->column_list = array("card_number","card_iccid");
        $card_list = $excel->get_excel_data($file_type);
        if(empty($card_list)){
            $this->error("文件数据为空！");exit;
        }
        $error_list = array();
        $card_import_batch = date("YmdHis").rand(1000,9999);
        foreach($card_list AS $cindex => $citem){
            if(!empty($citem['card_number']) && !empty($citem['card_iccid'])) {
                $card = $this->m_card->where(array("card_number" => $citem["card_number"]))->find();
                if (!empty($card)) {
                    $error_list[] = "第" . ($cindex + 2) . "行:<" . $citem['card_number'] . ">已存在";
                }else {
                    $insert = array(
                        "card_import_batch" => $card_import_batch,
                        "card_number" => $citem["card_number"],
                        "card_iccid" => $citem["card_iccid"],
                        "create_time" => time(),
                        "card_import_status" => 0,
                    );
                    if (is_numeric($citem['card_number']) && strlen($citem["card_number"]) == 13) {
                        $citem["uid"] = session("ADMIN_ID");
                        $citem["create_uid"] = session("ADMIN_ID");
                        $citem["package_id_month"] = 1;
                        $package = $this->m_package->where(array("package_id" => 1))->find();
                        $citem["package_data_month"] = $package["package_data"];
                        $citem["package_endtime"] = (strtotime(date("Y-m-01 00:00:00")."+12 month")-1);
                        $result = $this->m_card->add($citem);
                        if (!$result) {
                            $error_list[] = "第" . ($cindex + 2) . "行:<" . $citem['card_number'] . ">数据导入失败";
                        } else {
                            $insert["card_import_status"] = 1;
                        }
                    } else {
                        $error_list[] = "第" . ($cindex + 2) . "行:<" . $citem['card_number'] . ">数据格式有误";
                    }
                    $result = $this->m_card_import->add($insert);
                    addOrder($this->wl_config['flow_pool_id'], $citem["card_number"], 1, 1);
                }
            }else{
                $error_list[] = "第".($cindex+2)."行:<".$citem['card_number'].">数据为空";
            }

        }
        addLog(1,"卡批量导入");
        $this->assign("error_list", $error_list);
        $this->display("AdminCard/import_success");
    }


    /**
     * 导入历史
     */
    function import_history(){
        $s_show = I("s_show");
        if(empty($s_show)){
            $s_show = 1;
        }

        $s_start_time = I("s_start_time");
        $s_end_time = I("s_end_time");
        $s_number = I("s_number");
        if(empty($s_start_time)){
            $s_start_time = date("Y-m-01");
        }
        if(empty($s_end_time)){
            $s_end_time = date("Y-m-d");
        }
        $str_start_time = strtotime($s_start_time." 00:00:00");
        $str_end_time = strtotime($s_end_time."23:59:59");

        $where = array(
            "create_time"=>array("between",array($str_start_time,$str_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }

        if($s_show == 1){
            $success_num = 0;
            $fail_num = 0;
            $card_import_list = $this->m_card_import->where($where)->select();
            foreach($card_import_list AS $citem){
                if($citem["card_import_status"] == 1){
                    $success_num++;
                }else{
                    $fail_num++;
                }
            }
            $this->assign("success_num",$success_num);
            $this->assign("fail_num",$fail_num);
        }else{
            $count=$this->m_card_import->where($where)->count();
            $page = $this->page($count, 15);
            $card_import_list = $this->m_card_import->where($where)
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $this->assign("page", $page->show('Admin'));
        }
        foreach($card_import_list AS &$citem){
            $citem["create_time"] = date("Y-m-d H:i:s");
            if($citem["card_import_status"] == 1){
                $citem["card_import_status"] = "导入成功";
            }else{
                $citem["card_import_status"] = "导入失败";
            }
        }

        //设置导出数据
        $export_title = array(
            "card_number"=>"卡号",
            "card_iccid"=>"iccid",
            "create_time"=>"导入时间",
            "card_import_status"=>"激活状态",
        );

        $export_list = array(
            "excel_title"=>"卡导入历史",
            "export_title"=>$export_title,
            "export_data"=>$card_import_list,
        );
        write_file("import_history",$export_list);

        $this->assign("card_import_list",$card_import_list);
        $this->assign("s_number",$s_number);
        $this->assign("s_show",$s_show);
        $this->assign("s_start_time",$s_start_time);
        $this->assign("s_end_time",$s_end_time);
        $this->assign("a", "#F{\$dp.\$D('s_end_time')}");
        $this->assign("b", "#F{\$dp.\$D('s_start_time')}");
        $this->display();
    }



    /**
     * 活卡激活
     */
    public function active(){

        $this->display();
    }

    /**
     * 活卡激活提交
     */
    public function active_post(){
        $file = $_FILES['card_file']['tmp_name'];
        $file_type=$_FILES['card_file']['type'];

        if(!in_array($file_type,getExcelType())){
            $this->error("文件格式有误！");exit;
        }
        if(empty($file)){
            $this->error("文件不存在！");exit;
        }
        $excel = new \Common\Lib\excel($file);
        $excel->column_list = array("card_iccid");
        $card_list = $excel->get_excel_data($file_type);
        if(empty($card_list)){
            $this->error("文件数据为空！");exit;
        }
        $error_list = array();
        $card_activate_batch = date("YmdHis").rand(100,999);
        $telecom = new \Wulian\Lib\telecom();
        foreach($card_list AS $cindex => $citem){
            if(!empty($citem['card_iccid'])){
                $citem["card_iccid"] = trim($citem["card_iccid"]);
                $card_number = getNumberByIccid($citem['card_iccid']);
                $insert = array(
                    "card_activate_batch"=>$card_activate_batch,
                    "card_number"=>$card_number,
                    "create_time"=>time(),
                    "card_iccid"=>$citem['card_iccid'],
                );
                if(empty($card_number)){
                    $insert["card_activate_status"] = 0;
                    $error_list[] = "第".($cindex+2)."行:<".$citem['card_iccid'].">尚未导入";
                }elseif(strlen($citem['card_iccid']) != 20){
                    $insert["card_activate_status"] = 0;
                    $error_list[] = "第".($cindex+2)."行:<".$citem['card_iccid'].">数据格式有误";
//                    $result = false;
                }else{
                    $result = $telecom->requestServActive($card_number);
                    if(isset($result['RESULT']) && $result['RESULT'] == '0'){
                        $insert["card_activate_status"] = 1;
                    }else{
                        $insert["card_activate_status"] = 0;
                        $error_list[] = "第".($cindex+2)."行:<".$citem['card_iccid']."> ".$result["SMSG"];
                    }

                }

                $this->m_card_activate->add($insert);
                if($insert["card_activate_status"] == 1){
                    $this->m_card->where(array("card_iccid"=>$citem["card_iccid"]))->save(array("card_activate"=>1,"card_activate_time"=>time(),"card_status"=>1));
                }

            }

        }
        addLog(1,"活卡激活 批量激活");
        $this->assign("error_list", $error_list);
        $this->display("AdminCard/import_success");
    }

    /**
     * 激活历史
     */
    function active_history(){
        $s_show = I("s_show");
        if(empty($s_show)){
            $s_show = 1;
        }

        $s_start_time = I("s_start_time");
        $s_end_time = I("s_end_time");
        $s_number = I("s_number");
        if(empty($s_start_time)){
            $s_start_time = date("Y-m-01");
        }
        if(empty($s_end_time)){
            $s_end_time = date("Y-m-d");
        }
        $str_start_time = strtotime($s_start_time." 00:00:00");
        $str_end_time = strtotime($s_end_time."23:59:59");

        $where = array(
            "create_time"=>array("between",array($str_start_time,$str_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }


        if($s_show == 1){
            $success_num = 0;
            $fail_num = 0;
            $card_active_list = $this->m_card_activate->where($where)->select();
            foreach($card_active_list AS $citem){
                if($citem["card_activate_status"] == 1){
                    $success_num++;
                }else{
                    $fail_num++;
                }
            }
            $this->assign("success_num",$success_num);
            $this->assign("fail_num",$fail_num);
        }else{
            $count=$this->m_card_activate->where($where)->count();
            $page = $this->page($count, 15);
            $card_active_list = $this->m_card_activate->where($where)
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $this->assign("page", $page->show('Admin'));

            foreach($card_active_list AS &$citem){
                $citem["create_time"] = date("Y-m-d H:i:s",$citem["create_time"]);
                if($citem["card_activate_status"] == 1){
                    $citem["card_activate_status"] = "成功收到激活消息";
                }else{
                    $citem["card_activate_status"] = "激活失败";
                }
            }

            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "card_number"=>"接入号码",
                "create_time"=>"激活发起时间",
                "card_activate_status"=>"激活状态",
            );

            $export_list = array(
                "excel_title"=>"激活历史详单",
                "export_title"=>$export_title,
                "export_data"=>$card_active_list,
            );
            write_file("active_history",$export_list);
        }



        $this->assign("card_active_list",$card_active_list);
        $this->assign("s_number",$s_number);
        $this->assign("s_show",$s_show);
        $this->assign("s_start_time",$s_start_time);
        $this->assign("s_end_time",$s_end_time);
        $this->assign("a", "#F{\$dp.\$D('s_end_time')}");
        $this->assign("b", "#F{\$dp.\$D('s_start_time')}");
        $this->display();
    }

    /**
     * 卡绑定列表
     */
    function bind_list(){
        $s_type = I("s_type");
        $s_number = I("s_number");
        $s_uid = I("s_uid");
        $where = array();
        $_GET['s_uid'] = $s_uid;
        if(!empty($s_number)){
            $_GET['s_number'] = $s_number;
            $this->assign("s_number", $s_number);
            if($s_type == 1){
                $where['c.card_iccid'] = $s_number;
                checkCardNumber($s_number);
            }else{
                $where['c.card_iccid'] = array('like',"%".$s_number."%");
            }

        }
        if(!empty($s_uid)){
            $_GET['s_uid'] = $s_uid;
            $this->assign("s_uid", $s_uid);
            $where['c.uid'] = $s_uid;
        }

        $count=$this->m_card->alias("c")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->count();
        $page = $this->page($count, 10);
        $list = $this->m_card->alias("c")->field("c.card_number,c.card_iccid,u.user_nickname")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->limit($page->firstRow , $page->listRows)->select();

        $user_list = $this->m_user->where(array("user_type"=>2))->select();
        $this->assign("user_list", $user_list);

        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    /**
     * 批量绑定
     */
    function bind(){
        if(I("post.is_post") == 1){
            $uid = I("post.uid");
            if(empty($uid)){
                $this->error("请选择绑定对象！");exit;
            }
            $file = $_FILES['card_file']['tmp_name'];
            $file_type=$_FILES['card_file']['type'];
            if(!in_array($file_type,getExcelType())){
                $this->error("文件格式有误！");exit;
            }
            if(empty($file)){
                $this->error("文件不存在！");exit;
            }
            $excel = new \Common\Lib\excel($file);
            $excel->column_list = array("card_iccid");
            $card_list = $excel->get_excel_data($file_type);
            if(empty($card_list)){
                $this->error("文件数据为空！");exit;
            }
            $error_list = array();
            foreach($card_list AS $cindex => $citem){
                $card = $this->m_card->where(array("card_iccid"=>trim($citem['card_iccid'])))->find();
                if(empty($card)){
                    $error_list[] = "第".($cindex+2)."行:<".$citem["card_iccid"].">尚未导入";
                }else{
                    $result =  $this->m_card->where(array("card_iccid"=>trim($citem['card_iccid'])))->save(array("uid"=>$uid));
                    if(!$result){
                        $error_list[] = "第".($cindex+2)."行:<".$citem["card_iccid"].">更新数据失败";
                    }
                }

            }
            addLog(1,"卡绑定 批量绑定");

            $this->assign("error_list", $error_list);
            $this->display("AdminCard/import_success");exit;

        }
        $user_list = $this->m_user->where(array("user_type"=>2))->select();
        $this->assign("user_list", $user_list);
        $this->display();
    }

    /**
     * 卡状态查询
     */
    function status(){
        $list = array();
        if(isset($_POST["s_number"]) || isset($_GET["s_number"])) {
            $checkCard = "";
            $s_number = I("s_number");
            $_GET["s_number"] = $s_number;
            $number_list = array();
            $list = array();
            $where = array();
            if (!empty($s_number)) {
                $_GET['s_number'] = $s_number;
                $this->assign("s_number", $s_number);
                $number_list = explode(",", $s_number);
                $where["card_iccid"] = array("IN",$number_list);
                $checkCard = checkCardNumberReturnTxt($number_list);
            }
            $count = $this->m_card->where($where)->count();
            $page = $this->page($count, 10);
            $card_list = $this->m_card->field("card_number,card_iccid")->where($where)->limit($page->firstRow, $page->listRows)->select();
            $pageHtml =  $page->show('Admin');
            $pageHtml = getAjaxPageHtml($pageHtml);

            $telecom = new \Wulian\Lib\telecom();
            foreach ($card_list AS $nitem) {
                $temp = array();
                $data = $telecom->getQueryCardStatus($nitem["card_number"]);
                $temp["card_iccid"] = $nitem["card_iccid"];
                $temp['card_number'] = $nitem["card_number"];
                $temp['status_name'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusName'];
                if (!is_array($data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'])) {
                    $create_date = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'];
                    $create_date = substr($create_date, 0, 4) . "-" . substr($create_date, 4, 2) . "-" . substr($create_date, 6, 2);
                    $temp['create_date'] = $create_date;
                } else {
                    $temp['create_date'] = "--";
                }
                $list[] = $temp;
            }

            //设置导出数据
            $export_title = array(
                "card_iccid" => "ICCID",
                "card_number" => "接入号码",
                "create_date" => "激活日期",
                "status_name" => "UIM卡状态",
            );

            $export_list = array(
                "excel_title" => "UIM卡状态",
                "export_title" => $export_title,
                "export_data" => $list,
            );
            write_file("card_status", $export_list);
            successJson($list,$pageHtml,$checkCard);
        }
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 停机保号
     */
    function disabled(){
        $s_number = I("s_number");

        $list = array();
        if(isset($_POST["s_number"]) || isset($_GET["s_number"])) {
            $checkCard = "";
            $where = array();
            if (!empty($s_number)) {
                $_GET['s_number'] = $s_number;
                $this->assign("s_number", $s_number);
                $number_list = explode(",", $s_number);
                $where["card_iccid"] = array("IN",$number_list);
                $checkCard = checkCardNumberReturnTxt($number_list);
            }
            $count = $this->m_card->where($where)->count();
            $page = $this->page($count, 10);
            $card_list = $this->m_card->field("card_number,card_iccid")->where($where)->limit($page->firstRow, $page->listRows)->select();
            $pageHtml =  $page->show('Admin');
            $pageHtml = getAjaxPageHtml($pageHtml);

            $telecom = new \Wulian\Lib\telecom();
            foreach ($card_list AS $nitem) {
                $temp = array();
                $data = $telecom->getQueryCardStatus($nitem["card_number"]);
                $temp["card_iccid"] = $nitem["card_iccid"];
                $temp['card_number'] = $nitem["card_number"];
                $temp['status_name'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusName'];
                $temp['status_code'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusCd'];
                if (!is_array($data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'])) {
                    $create_date = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'];
                    $create_date = substr($create_date, 0, 4) . "-" . substr($create_date, 4, 2) . "-" . substr($create_date, 6, 2);
                    $temp['create_date'] = $create_date;
                } else {
                    $temp['create_date'] = "--";
                }
                $list[] = $temp;
            }
            //设置导出数据
            $export_title = array(
                "card_iccid" => "ICCID",
                "card_number" => "接入卡号",
                "create_date" => "激活日期",
                "status_name" => "产品状态",
            );

            $export_list = array(
                "excel_title" => "停机保号管理",
                "export_title" => $export_title,
                "export_data" => $list,
            );
            write_file("card_disabled", $export_list);
            successJson($list,$pageHtml,$checkCard);
        }
        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 停复机调用接口
     */
    function _disable_option($card_number,$orderTypeId,$card_status_batch = ""){
        $result = array();
        $disable_status = array(
            19=>0,
            20=>1
        );
        if(empty($card_status_batch)){
            $card_status_batch = date("YmdHis").rand(100,999);
        }
        $card = $this->m_card->where(array("card_number"=>$card_number))->find();
        $insert = array(
            "card_status_batch"=>$card_status_batch,
            "card_number"=>$card_number,
            "card_iccid"=>$card["card_iccid"],
            "card_status_type"=>$disable_status[$orderTypeId],
            "create_time"=>time()
        );

        $telecom = new \Wulian\Lib\telecom();
        $data = $telecom->disabledNumber($card_number,$orderTypeId);
        if($data["result"] == '0'){
            $insert["card_status_status"] = 1;
            $this->m_card_status->add($insert);
            $this->m_card->where(array("card_number"=>$card_number))->save(array("card_status"=>$disable_status[$orderTypeId]));
            $result['result'] = true;
        }else{
            $insert["card_status_status"] = 0;
            $this->m_card_status->add($insert);
            $result["result"] = false;
            $result['message'] = $data["resultMsg"];
        }
        return $result;
    }

    /**
     * 单个停复机操作
     */
    function one_disabled(){
        $model = I("model");    //20 复机  19停机
        $card_number = I('card_number');
        if(!in_array($model,array(19,20))){
            $this->error("参数有误!");exit;
        }
        if(empty($card_number)){
            $this->error("卡号为空!");exit;
        }
        $result = $this->_disable_option($card_number,$model);
        if($result['result']){
            $disabled = array(
                20=>'复机',
                19=>'停机',
            );
            addLog(3,"停机保号 ",$disabled[$model]);
            $this->success("操作成功!",U('AdminCard/disabled'));
        }else{
            $this->success("操作失败:".$result['message'],U('AdminCard/disabled'));
        }

    }

    /**
     * 批量停复机操作
     */
    function disabled_batch(){
        $batch_import = I("post.batch_import");
        if($batch_import == 1){
            $file = $_FILES['card_file']['tmp_name'];
            $file_type=$_FILES['card_file']['type'];

            if(!in_array($file_type,getExcelType())){
                $this->error("文件格式有误！");exit;
            }
            if(empty($file)){
                $this->error("文件不存在！");exit;
            }
            $excel = new \Common\Lib\excel($file);
            $excel->column_list = array("card_iccid","card_status");
            $card_list = $excel->get_excel_data($file_type);
            if(empty($card_list)){
                $this->error("文件数据为空！");exit;
            }
            $error_list = array();
            $card_activate_batch = date("YmdHis").rand(100,999);
            foreach($card_list AS $cindex => $citem){
                if(!empty($citem['card_iccid'])){
                    $card_number = getNumberByIccid($citem['card_iccid']);
                    if(!empty($card_number)){
                        $result = $this->_disable_option($card_number,$citem["card_status"],$card_activate_batch);
                        if(!$result['result']){
                            $error_list[] = "第".($cindex+2)."行:<".$citem['card_number'].">".$result['message'];
                        }
                    }else{
                        $error_list[] = "第".($cindex+2)."行:<".$citem['card_number'].">尚未导入";
                    }


                }
            }

            addLog(3,"停机保号 批量停机复机");
            $this->assign("error_list", $error_list);
            $this->display("AdminCard/import_success");exit;
        }
        $this->display();
    }

    /**
     * 停复机历史查询
     */
    function disabled_history(){
        $s_show = I("s_show");
        if(empty($s_show)){
            $s_show = 1;
        }

        $s_start_time = I("s_start_time");
        $s_end_time = I("s_end_time");
        $s_number = I("s_number");
        if(empty($s_start_time)){
            $s_start_time = date("Y-m-01");
        }
        if(empty($s_end_time)){
            $s_end_time = date("Y-m-d");
        }
        $str_start_time = strtotime($s_start_time." 00:00:00");
        $str_end_time = strtotime($s_end_time."23:59:59");

        $where = array(
            "create_time"=>array("between",array($str_start_time,$str_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }


        if($s_show == 1){
            $success_num = 0;
            $fail_num = 0;
            $card_status_list = $this->m_card_status->where($where)->select();
            foreach($card_status_list AS $citem){
                if($citem["card_status_status"] == 1){
                    $success_num++;
                }else{
                    $fail_num++;
                }
            }
            $this->assign("success_num",$success_num);
            $this->assign("fail_num",$fail_num);
        }else{
            $count=$this->m_card_status->where($where)->count();
            $page = $this->page($count, 15);
            $card_status_list = $this->m_card_status->where($where)
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $this->assign("page", $page->show('Admin'));

            foreach($card_status_list AS &$citem){
                $citem["create_time"] = date("Y-m-d H:i:s",$citem["create_time"]);
                if($citem["card_status_status"] == 1){
                    $citem["card_status_status"] = "服务器成功收到请求消息";
                }else{
                    $citem["card_status_status"] = "请求失败";
                }
            }

            if(!empty($card_status_list)){
                //设置导出数据
                $export_title = array(
                    "card_iccid" => "ICCID",
                    "card_number" => "接入卡号",
                    "create_time" => "停复机时间",
                    "card_status_status" => "停复机状态",
                );

                $export_list = array(
                    "excel_title" => "停复机历史",
                    "export_title" => $export_title,
                    "export_data" => $card_status_list,
                );
                write_file("card_disabled_history", $export_list);
            }
        }





        $this->assign("card_status_list",$card_status_list);
        $this->assign("s_number",$s_number);
        $this->assign("s_show",$s_show);
        $this->assign("s_start_time",$s_start_time);
        $this->assign("s_end_time",$s_end_time);
        $this->assign("a", "#F{\$dp.\$D('s_end_time')}");
        $this->assign("b", "#F{\$dp.\$D('s_start_time')}");
        $this->display();
    }

    /**
     * 商户卡查询
     */
    function merchant(){
        $s_number = I("s_number");
        $s_uid = I("s_uid");
        $list = array();
        $s_start_time = isset($_GET["s_start_time"])?$_GET["s_start_time"]:date("Y-m");
        $this->assign("s_start_time", $s_start_time);

        if(isset($_POST['s_number']) || isset($_GET['s_number'])){
            $checkCard = "";
            $time_arr = explode("-",$s_start_time);
            $where = array("flow_record_year"=>$time_arr[0],"flow_record_month"=>$time_arr[1]);
            if(!empty($s_number)) {
                $_GET['s_number'] = $s_number;
                $where["r.card_iccid"] = array("IN",explode(",",$s_number));
                $this->assign("s_number", $s_number);
                $checkCard = checkCardNumberReturnTxt($s_number);
            }
            if(!empty($s_uid)){
                $_GET['s_uid'] = $s_uid;
                $this->assign("s_uid", $s_uid);
                $where['c.uid'] = $s_uid;
            }

            $count=$this->m_flow_record->alias("r")->join("cmf_wl_card c ON c.card_number = r.card_number","INNER")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->count();
            $page = $this->page($count, 10);
            $list = $this->m_flow_record->alias("r")->field("u.user_nickname,c.card_number,c.card_iccid,r.package_data_month,r.package_data_add")->join("cmf_wl_card c ON c.card_number = r.card_number","INNER")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->limit($page->firstRow , $page->listRows)->select();
            $pageHtml =  $page->show('Admin');
            $pageHtml = getAjaxPageHtml($pageHtml);

            $telecom = new \Wulian\Lib\telecom();
            $telecom->poolNbr = $this->wl_config['flow_pool_id'];
            foreach($list AS &$nitem){
                $nitem["use_flow"] = 0;
                $card_use = $telecom->getPoolMemQry(1,$nitem["card_number"]);

                if(isset($card_use['PoolInfo'])){
                    $card_use['PoolInfo']['pool_left'] = $card_use['pool_left'];
                    $use_flow = $card_use['PoolInfo']["POOL_MEMBER_ALREADY"];
                    $nitem["use_flow"] = $use_flow;
                }
                if($this->c_proportion ==1 ){   //代理商版本
                    $plus_flow = kbToM($card_use["PoolInfo"]["POOL_MEMBER_TOTAL"]-$card_use["PoolInfo"]["POOL_MEMBER_ALREADY"])*$this->wl_proportion;
                }else{
                    $plus_flow = kbToM($card_use["PoolInfo"]["POOL_MEMBER_TOTAL"]-$card_use["PoolInfo"]["POOL_MEMBER_ALREADY"]);
                }

                $nitem["plus_flow"] = $plus_flow."M";
                $nitem["package_data_month"] = ($nitem["package_data_month"]/$this->c_proportion)."M";
                $nitem["package_data_add"] = ($nitem["package_data_add"]/$this->c_proportion)."M";
            }

            //设置导出数据
            $export_title = array(
//                "user_nickname"=>"所属用户",
                "card_iccid"=>"ICCID",
                "card_number"=>"卡号",
                "plus_flow"=>"当月剩余流量",
                "package_data_month"=>"当月月套餐",
                "package_data_add"=>"当月增量包",
            );

            $export_list = array(
                "excel_title"=>"商户卡流量查询",
                "export_title"=>$export_title,
                "export_data"=>$list,
            );
            write_file("card_merchant",$export_list);
            successJson($list,$pageHtml,$checkCard);
        }

        $user_list = $this->m_user->where(array("user_type"=>2))->select();
        $this->assign("user_list", $user_list);

        $this->assign("list", $list);
        $this->display();
    }

    /**
     * 查看详情
     */
    function detail(){

        $card_number = I("card_number");
        $this->assign("card_number", $card_number);
        $list = array();
        $time = I("time");
        $timeArr = explode("-",$time);

        $BeginDate=$time."-01";
        $EndDate = strtotime("$BeginDate +1 month -1 day");
        if($EndDate>time()){
            $EndDate = time();
        }
        $time_end =  date('Ymd', $EndDate);

        $telecom = new \Wulian\Lib\telecom();
        $traffic = $telecom->queryTrafficByDate($card_number,$timeArr[0].$timeArr[1]."01",$time_end);
        $traffic = $traffic["NEW_DATA_TICKET_QRlist"];

        foreach($traffic AS $titem){
            $titem = (array)$titem;

            if(session("proportion") != 1){ //代理商版本
                $titem["BYTES_CNT"] = str_replace("KB","",$titem["BYTES_CNT"]);
                $titem["BYTES_CNT"] = round($titem["BYTES_CNT"]*$this->wl_proportion,0)."KB";
            }

            $list[] = $titem;
        }
//        echo "<pre>";
//        print_r($list);exit;
        $this->assign("list", $list);
        $this->display();
    }


}